package org.dromara.system.kernel;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.system.domain.PayApp;
import org.dromara.system.service.IPayAppService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;

@Service
@RequiredArgsConstructor
public class PayAppKernel {

    private final IPayAppService payAppService;

    /**
     * 获取应用
     *
     * @param appId     订单参数
     * @param appSecret 应用密匙
     * @return 应用
     */
    public R<PayApp> getPayApp(Long appId, String appSecret) {
        PayApp app = payAppService.selectById(appId);
        if (app == null || app.getIsDelete() == 1) {
            return R.fail("The appId is incorrect");//appId有误
        }
        //校验应用密匙
        if (!app.getAppSecret().equals(appSecret)) {
            return R.fail("The appSecret is incorrect, params field [Secret-Key]");//应用密匙【Secret-Key】有误
        }
        //检查是否在线
        if (app.getStatus() != 1) {
            return R.fail("This app is offline");//此应用已下线
        }
        //应用超过收款限额，不允许收款
        if (app.getTotalLimit() != null && app.getTotalLimit().compareTo(BigDecimal.ZERO) > 0 && app.getSuccessTotal() != null && app.getSuccessTotal().compareTo(app.getTotalLimit()) >= 0) {
            return R.fail("This app payment limit has been exceeded.");//超过收款限额   and this merchant has been offline 此商户已下线
        }
        if (app.getGroupId() == null) {
            return R.fail("No bind pay group");
        }
        return R.ok(app);
    }
}
